package com.times.web.system.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.times.core.base.BaseResult;
import com.times.core.common.controller.BaseController;

/**
 * Created by zhiyuan on 2017/7/25.
 */
@Controller
@RequestMapping("/")
public class LoginController extends BaseController{

    @RequestMapping("login")
    public String login() throws  Exception{
        log.info("111");
        return "login";
    }

    /**
     * 登录方法，shiro验证
     * @param request
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "doLogin")
    @ResponseBody
    public BaseResult doLogin(HttpServletRequest request) throws Exception{
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String validatorCode = request.getParameter("validatorCode");
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username, password);
        try {
            subject.login(usernamePasswordToken);
        }catch(Exception ex){
            log.warn(ex.getMessage());
            throw ex;
        }
        return new BaseResult(200, "", null);
    }

    /**
     * 登出
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "logout")
    public String doLogout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "login";
    }


    @RequestMapping("showMe")
    @ResponseBody
    public BaseResult showMe(){
        return BaseResult.optSuccess();
    }

    @RequestMapping(method = RequestMethod.GET, value = "index")
    public String index(){
        return "index";
    }
}
